package com.xhd.file;

import java.io.File;
import java.util.HashMap;
import java.util.Vector;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.base.util.Date;
import com.xhd.cache.CacheMgr;
import com.xhd.cache.UseCache;

public class XDecompressedFileCache 
	  implements UseCache
	{
	  private static XDecompressedFileCache jm16 = null;
	  private static final Log jm9;
	  private String jm320 = null;
	  private HashMap jm321 = null;
	  private int jm317 = 1024;
	  private int jm322 = 134217728;
	  private final int jm323 = 1800000;
	  static Class jm10;

	  static
	  {
	    Class tmp7_4 = jm10;
	    if (tmp7_4 == null)
	    {
	     
	      try
	      {
	        XDecompressedFileCache.jm10 = Class.forName("com.sinocc.base.file.XDecompressedFileCache");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp7_4);
	  }

	  public static XDecompressedFileCache getInstance()
	  {
	    return jm16;
	  }

	  public static boolean canCache()
	  {
	    return (jm16 != null);
	  }

	  public static boolean createCache(String paramString)
	  {
	    if (jm16 == null)
	    {
	      jm9.debug("XDecompressedFileCache create on " + paramString);
	      jm16 = new XDecompressedFileCache();
	      jm16.jm320 = paramString;
	      File localFile = new File(paramString);
	      if (localFile.exists())
	      {
	        File[] arrayOfFile = localFile.listFiles();
	        for (int i = 0; i < arrayOfFile.length; ++i)
	          arrayOfFile[i].delete();
	      }
	      else
	      {
	        localFile.mkdirs();
	      }
	      return true;
	    }
	    return false;
	  }

	  private XDecompressedFileCache()
	  {
	    CacheMgr.registerCache(this);
	  }

	  public void setMax(int paramInt)
	  {
	    this.jm317 = paramInt;
	  }

	  public int getMax()
	  {
	    return this.jm317;
	  }

	  public Object cacheData()
	  {
	    return this.jm321;
	  }

	  public int cacheSize()
	  {
	    return this.jm321.size();
	  }

	  public boolean clearCache()
	  {
	    this.jm321.clear();
	    return true;
	  }

	  private String jm88(String paramString)
	  {
	    int i = paramString.length() - 1;
	    StringBuffer localStringBuffer = new StringBuffer(64);
	    while (i >= 0)
	    {
	      char c = paramString.charAt(i);
	      if ('\\' == c)
	        break;
	      if ('/' == c)
	        break;
	      localStringBuffer.append(c);
	      --i;
	    }
	    return localStringBuffer.toString();
	  }

	  private synchronized void jm319()
	  {
	    long l = System.currentTimeMillis() - 1800000L;
	    Vector localVector = new Vector(this.jm321.values());
	    int i = localVector.size();
	    Object localObject = null;
	    int j = 0;
	    for (int k = 0; k < i; ++k)
	    {
	      Item localItem = (Item)localVector.get(k);
	      if ((localObject == null) || ((Item)localItem.Hit <(Item)localObject.Hit))
	        localObject = localItem;
	      if (l <= localItem.LastTime)
	        continue;
	      ++j;
	      this.jm321.remove(localItem.FileName);
	      File localFile2 = new File(localItem.FilePath);
	      if (!(localFile2.exists()))
	        continue;
	      localFile2.delete();
	    }
	    if ((j >= 1) || (localObject == null))
	      return;
	    this.jm321.remove((Item)localObject.FileName);
	    File localFile1 = new File(localObject.FilePath);
	    if (localFile1.exists())
	      localFile1.delete();
	    jm9.debug("XDecompressedFileCache remove: " +(Item) localObject.FileName);
	  }

	  public String getFile(String paramString, Date paramDate)
	  {
	    String str = jm88(paramString);
	    Item localItem = (Item)this.jm321.get(str);
	    if (localItem == null)
	      return null;
	    if (localItem.FileTime != paramDate.getTime())
	    {
	      this.jm321.remove(str);
	      File localFile = new File(localItem.FilePath);
	      if (localFile.exists())
	        localFile.delete();
	      return null;
	    }
	    localItem.Hit += 1;
	    localItem.LastTime = System.currentTimeMillis();
	    return localItem.FilePath;
	  }

	  public String getFile(String paramString1, String paramString2)
	  {
	    if ((paramString2 == null) || (paramString2.length() < 10))
	      return null;
	    return getFile(paramString1, new Date(paramString2));
	  }

	  public String cacheFile(String paramString1, String paramString2)
	  {
	    if ((paramString2 == null) || (paramString2.length() < 10))
	      return null;
	    return cacheFile(paramString1, new Date(paramString2));
	  }

	  public String cacheFile(String paramString, Date paramDate)
	  {
	    String str1 = jm88(paramString);
	    String str2 = this.jm320;
	    str2 = str2 + str1;
	    boolean bool = XCompressedFile.getInstance().decompressFile(paramString, str2);
	    if (bool)
	    {
	      File localFile = new File(str2);
	      if (localFile.length() > this.jm322)
	        jm9.warn("Warn: XDecompressedFileCache file size tool large " + localFile.length());
	      if (this.jm321.size() >= this.jm317)
	        jm319();
	      Item localItem = new Item(null);
	      localItem.FileName = str1;
	      localItem.FilePath = str2;
	      localItem.FileTime = paramDate.getTime();
	      localItem.Hit = 0;
	      localItem.LastTime = System.currentTimeMillis();
	      this.jm321.put(str1, localItem);
	    }
	    else
	    {
	      str2 = null;
	      jm9.warn("XDecompressedFileCache cache file decompress failed: " + paramString);
	    }
	    return str2;
	  }

	  private class Item
	  {
	    public String FileName;
	    public String FilePath;
	    public long FileTime;
	    public int Hit;
	    public long LastTime;
	    final XDecompressedFileCache jm19;

	    private Item()
	    {
	    //  this.jm19 =  ;
	    }

	    private  Item(Item paramItem)
	    {
	    //  this(this$1);
	    }
	  }
}
